//
//  KCMontnReportController.swift
//  MyKC
//
//  Created by yy on 2017/3/28.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit
import YYModel

class KCMontnReportController: UIViewController {

    var model = KCPlanModel()
    lazy var xunDianStatusArray = [KCXunDianStatusModel]()
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var lbe_storeName: UILabel!
    @IBOutlet weak var lbe_storeNo: UILabel!
    
    @IBOutlet weak var lbe_storeAddress: UILabel!
    
    lazy var headView: KCSalesWorkHeadView = {
        let head = KCSalesWorkHeadView.salesWorkHeadView()
        head.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 50)
        return head
    }()
    
    lazy var cellcontentArray = [["价格月报","画家月报"],["巡店问卷","竞品信息"]]
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        getData()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.subviews[0].alpha = 1
    }

}

// MARK: -  设置界面基本信息
extension KCMontnReportController {
    internal func setupUI() {
        lbe_storeName.text = model.StoreName
        lbe_storeNo.text = model.StoreNo
        lbe_storeAddress.text = model.Address
        
        //注册cell
        tableView.register(UINib(nibName: "KCSalesWorkSelectCell", bundle: nil), forCellReuseIdentifier: "KCSalesWorkSelectCellId")
        tableView.tableFooterView = UIView()
        
        //设置导航条
        navigationItem.title = "工作"

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "离店", style: .plain, target: self, action: #selector(liDianBtnCLick))
    }
    // MARK: -  离店按钮点击事件
    @objc private func liDianBtnCLick() {
        
    }
}

 // MARK: -  table的代理方法实现
extension KCMontnReportController: UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return xunDianStatusArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "KCSalesWorkSelectCellId", for: indexPath) as! KCSalesWorkSelectCell
        setupCell(indexPath: indexPath, cell: cell)
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return headView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let xunDianWenJuan = KCXunDianWenJuanController()
        xunDianWenJuan.model = model
        navigationController?.pushViewController(xunDianWenJuan, animated: true)
    }
    
    private func setupCell(indexPath: IndexPath,cell: KCSalesWorkSelectCell) {
        cell.selectionStyle = .none
        let model = self.xunDianStatusArray[indexPath.row]
        let imageName = cellcontentArray.first?[indexPath.row]
        
        //根据不同不同的督导或者dr来显示数据------
        cell.imv_xunDian.image = UIImage(named: imageName!)
        cell.lbe_xunDian.text = cellcontentArray.last?[indexPath.row]
        cell.lbe_status.text = model.Comment
        if (model.Finished! as NSString).integerValue == 0 {
            cell.lbe_status.textColor = colorRGB(r: 42, g: 149, b: 241)
        }else {
            cell.lbe_status.textColor = UIColor.lightGray
        }
    }
}

// MARK: -  获取网络数据
extension KCMontnReportController {
    internal func getData() {
        //根据进入的是督导还是dr来判断获取的数据类型-----
        //是督导----巡店问卷和竞品信息的完成情况
        if sharedType == 5000420 {
            let stid = "CtrlFeedBack_" + model.StoreId! + "_" + model.TaskPlanId!
            let jingPinStid = "CtrlCollection_" + model.StoreId! + model.TaskPlanId!
            let dic = ["CtrlIds":stid + ";" + jingPinStid]
            KCNetworkManager.sharedTools.request(method: .POST, urlString: "/api/CtrlMark/Query", parameters: dic, finished: {[weak self] (response, error) in
                guard let response = response as? NSDictionary, let responseContent = response["Content"] as? NSArray else {
                    return
                }
                self?.xunDianStatusArray = NSArray.yy_modelArray(with: KCXunDianStatusModel.self, json: responseContent) as? [KCXunDianStatusModel] ?? [KCXunDianStatusModel]()
                self?.tableView.reloadData()
            })
        }
        //是dr----获取销量提报和缺货上报的完成情况----
        else if sharedType == 5000430 {
            
        }
    }
}


